

#include "CWriteFile.h"
#include <stdio.h>
#ifdef _IRR_PSP_
#include <sys/types.h>
#include <sys/unistd.h>
#include <pspiofilemgr.h>
#endif
namespace irr
{
    namespace io
    {


        CWriteFile::CWriteFile(const c8* fileName, bool append)
            : FileSize(0)
        {
#ifdef _DEBUG
            setDebugName("CWriteFile");
#endif

            Filename = fileName;
            openFile(append);
        }



        CWriteFile::~CWriteFile()
        {
            if (File)
			{
#ifdef _IRR_PSP_
                sceIoClose(File);
#else
                fclose(File);
#endif
			}
        }



        //! returns if file is open
        inline bool CWriteFile::isOpen()
        {
            return File != 0;
        }



        //! returns how much was read
        s32 CWriteFile::write(const void* buffer, s32 sizeToWrite)
        {
            s32 result = 0;
            
			if (!isOpen())
                return result;
#ifdef _IRR_PSP_
            result = sceIoWrite(File, buffer, sizeToWrite);
#else
            result = fwrite(buffer, sizeof(char), sizeToWrite, File);
#endif
            return result;
        }



        //! changes position in file, returns true if successful
        //! if relativeMovement==true, the pos is changed relative to current pos,
        //! otherwise from begin of file
        bool CWriteFile::seek(s32 finalPos, bool relativeMovement)
        {
            bool result = false; 

            if (!isOpen())
                return result;

#ifdef _IRR_PSP_
            result=sceIoLseek(File, finalPos, relativeMovement ? PSP_SEEK_CUR : PSP_SEEK_SET) == 0;
#else
            result = fseek(File, finalPos, relativeMovement ? SEEK_CUR : SEEK_SET) == 0;
#endif
            return   result;
        }



        //! returns where in the file we are.
        s32 CWriteFile::getPos()
        {
            s32 result;
#ifdef _IRR_PSP_
            result = sceIoLseek(File, 0, PSP_SEEK_CUR);
#else
            result = ftell(File);

#endif

            return result;
        }



        //! opens the file
        void CWriteFile::openFile(bool append)
        {
#ifdef _IRR_PSP_

            if (Filename.size() == 0)
            {
                File = 0;
                return;
            }

            c8* FilePath;
            c8* RealPath;
            core::stringc file_name;


            FilePath = (c8*)Filename.c_str();

            if (FilePath[0]== '/')
            {

                file_name = "ms0:";
                file_name += FilePath;
                RealPath = (c8*)file_name.c_str();

            }
            else
            {
                if (strstr(FilePath, ":/") == 0)
                {
                    c8 WorkingDirectory[1024];
                    getcwd(WorkingDirectory, 1024);

                    if (WorkingDirectory[ strlen(WorkingDirectory) - 1 ] != '\\' &&
                        WorkingDirectory[ strlen(WorkingDirectory) - 1 ] != '/')
                    {

                        WorkingDirectory[ strlen(WorkingDirectory) + 1 ] = 0;
                        WorkingDirectory[ strlen(WorkingDirectory) ] = '/';

                    }


                    file_name = WorkingDirectory;
                    file_name += FilePath;
                    RealPath = (c8*)file_name.c_str();
                }
                else
                {
                    RealPath = FilePath;
                }
            }

            File = sceIoOpen(RealPath, append ? PSP_O_APPEND | PSP_O_WRONLY | PSP_O_CREAT :  PSP_O_WRONLY | PSP_O_CREAT, 0777);
            // printf("\n%s %d %s\n", Filename.c_str(), File, (append==true ? "true" :"false"));

            if (File)
            {
                // get FileSize
                FileSize = sceIoLseek(File, 0, PSP_SEEK_END);
                sceIoLseek(File, 0, PSP_SEEK_SET);
            }
#else
            if (Filename.size() == 0)
            {
                File = 0;
                return;
            }

            File = fopen(Filename.c_str(), append ? "ab" : "wb");

            if (File)
            {
                // get FileSize
                fseek(File, 0, SEEK_END);
                FileSize = ftell(File);
                fseek(File, 0, SEEK_SET);
            }

#endif
        }



        //! returns name of file
        const c8* CWriteFile::getFileName()
        {
            return Filename.c_str();
        }



        IWriteFile* createWriteFile(const c8* fileName, bool append)
        {
            CWriteFile* file = new CWriteFile(fileName, append);
            if (file->isOpen())
                return file;

            file->drop();
            return 0;
        }


    } // end namespace io
} // end namespace irr

